﻿package com.flashframeless.audio {
	
	/* Original code, by André Anaya - www.andreanaya.com */
	
	import flash.display.Loader;
	
	import flash.events.Event;
	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class SoundCheck {
		private static var _init:Boolean = false;
		private static var _soundEnabled:Boolean = true;
		
		public function SoundCheck():void{
			trace ("SoundControl is a static class and should not be instantiated.");
		}
		public static function init():void{
			if(!_init){
				var bytes:ByteArray = new ByteArray()
				bytes.writeByte(0);
				bytes.writeByte(0);
				bytes.writeByte(0);
				bytes.writeByte(0);
				
				var swf:ByteArray = new ByteArray()
				swf.writeByte(70);
				swf.writeByte(87);
				swf.writeByte(83);
				swf.writeByte(9);
				swf.writeByte(40);
				swf.writeByte(2);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(120);
				swf.writeByte(0);
				swf.writeByte(4);
				swf.writeByte(226);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(14);
				swf.writeByte(166);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(24);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(68);
				swf.writeByte(17);
				swf.writeByte(9);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(68);
				swf.writeByte(16);
				swf.writeByte(232);
				swf.writeByte(3);
				swf.writeByte(60);
				swf.writeByte(0);
				swf.writeByte(67);
				swf.writeByte(2);
				swf.writeByte(134);
				swf.writeByte(156);
				swf.writeByte(167);
				swf.writeByte(90);
				swf.writeByte(10);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(2);
				swf.writeByte(0);
				swf.writeByte(92);
				swf.writeByte(48);
				swf.writeByte(2);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(209);
				swf.writeByte(79);
				swf.writeByte(45);
				swf.writeByte(150);
				swf.writeByte(12);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(206);
				swf.writeByte(10);
				swf.writeByte(83);
				swf.writeByte(111);
				swf.writeByte(117);
				swf.writeByte(110);
				swf.writeByte(100);
				swf.writeByte(80);
				swf.writeByte(114);
				swf.writeByte(111);
				swf.writeByte(118);
				swf.writeByte(105);
				swf.writeByte(100);
				swf.writeByte(101);
				swf.writeByte(114);
				swf.writeByte(0);
				swf.writeByte(10);
				swf.writeByte(14);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(101);
				swf.writeByte(109);
				swf.writeByte(112);
				swf.writeByte(116);
				swf.writeByte(121);
				swf.writeByte(0);
				swf.writeByte(191);
				swf.writeByte(20);
				swf.writeByte(164);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(102);
				swf.writeByte(114);
				swf.writeByte(97);
				swf.writeByte(109);
				swf.writeByte(101);
				swf.writeByte(49);
				swf.writeByte(0);
				swf.writeByte(16);
				swf.writeByte(0);
				swf.writeByte(46);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(15);
				swf.writeByte(0);
				swf.writeByte(13);
				swf.writeByte(83);
				swf.writeByte(111);
				swf.writeByte(117);
				swf.writeByte(110);
				swf.writeByte(100);
				swf.writeByte(80);
				swf.writeByte(114);
				swf.writeByte(111);
				swf.writeByte(118);
				swf.writeByte(105);
				swf.writeByte(100);
				swf.writeByte(101);
				swf.writeByte(114);
				swf.writeByte(13);
				swf.writeByte(102);
				swf.writeByte(108);
				swf.writeByte(97);
				swf.writeByte(115);
				swf.writeByte(104);
				swf.writeByte(46);
				swf.writeByte(100);
				swf.writeByte(105);
				swf.writeByte(115);
				swf.writeByte(112);
				swf.writeByte(108);
				swf.writeByte(97);
				swf.writeByte(121);
				swf.writeByte(6);
				swf.writeByte(83);
				swf.writeByte(112);
				swf.writeByte(114);
				swf.writeByte(105);
				swf.writeByte(116);
				swf.writeByte(101);
				swf.writeByte(5);
				swf.writeByte(115);
				swf.writeByte(111);
				swf.writeByte(117);
				swf.writeByte(110);
				swf.writeByte(100);
				swf.writeByte(9);
				swf.writeByte(83);
				swf.writeByte(111);
				swf.writeByte(117);
				swf.writeByte(110);
				swf.writeByte(100);
				swf.writeByte(73);
				swf.writeByte(116);
				swf.writeByte(101);
				swf.writeByte(109);
				swf.writeByte(11);
				swf.writeByte(102);
				swf.writeByte(108);
				swf.writeByte(97);
				swf.writeByte(115);
				swf.writeByte(104);
				swf.writeByte(46);
				swf.writeByte(109);
				swf.writeByte(101);
				swf.writeByte(100);
				swf.writeByte(105);
				swf.writeByte(97);
				swf.writeByte(5);
				swf.writeByte(83);
				swf.writeByte(111);
				swf.writeByte(117);
				swf.writeByte(110);
				swf.writeByte(100);
				swf.writeByte(6);
				swf.writeByte(79);
				swf.writeByte(98);
				swf.writeByte(106);
				swf.writeByte(101);
				swf.writeByte(99);
				swf.writeByte(116);
				swf.writeByte(12);
				swf.writeByte(102);
				swf.writeByte(108);
				swf.writeByte(97);
				swf.writeByte(115);
				swf.writeByte(104);
				swf.writeByte(46);
				swf.writeByte(101);
				swf.writeByte(118);
				swf.writeByte(101);
				swf.writeByte(110);
				swf.writeByte(116);
				swf.writeByte(115);
				swf.writeByte(15);
				swf.writeByte(69);
				swf.writeByte(118);
				swf.writeByte(101);
				swf.writeByte(110);
				swf.writeByte(116);
				swf.writeByte(68);
				swf.writeByte(105);
				swf.writeByte(115);
				swf.writeByte(112);
				swf.writeByte(97);
				swf.writeByte(116);
				swf.writeByte(99);
				swf.writeByte(104);
				swf.writeByte(101);
				swf.writeByte(114);
				swf.writeByte(13);
				swf.writeByte(68);
				swf.writeByte(105);
				swf.writeByte(115);
				swf.writeByte(112);
				swf.writeByte(108);
				swf.writeByte(97);
				swf.writeByte(121);
				swf.writeByte(79);
				swf.writeByte(98);
				swf.writeByte(106);
				swf.writeByte(101);
				swf.writeByte(99);
				swf.writeByte(116);
				swf.writeByte(17);
				swf.writeByte(73);
				swf.writeByte(110);
				swf.writeByte(116);
				swf.writeByte(101);
				swf.writeByte(114);
				swf.writeByte(97);
				swf.writeByte(99);
				swf.writeByte(116);
				swf.writeByte(105);
				swf.writeByte(118);
				swf.writeByte(101);
				swf.writeByte(79);
				swf.writeByte(98);
				swf.writeByte(106);
				swf.writeByte(101);
				swf.writeByte(99);
				swf.writeByte(116);
				swf.writeByte(22);
				swf.writeByte(68);
				swf.writeByte(105);
				swf.writeByte(115);
				swf.writeByte(112);
				swf.writeByte(108);
				swf.writeByte(97);
				swf.writeByte(121);
				swf.writeByte(79);
				swf.writeByte(98);
				swf.writeByte(106);
				swf.writeByte(101);
				swf.writeByte(99);
				swf.writeByte(116);
				swf.writeByte(67);
				swf.writeByte(111);
				swf.writeByte(110);
				swf.writeByte(116);
				swf.writeByte(97);
				swf.writeByte(105);
				swf.writeByte(110);
				swf.writeByte(101);
				swf.writeByte(114);
				swf.writeByte(8);
				swf.writeByte(22);
				swf.writeByte(1);
				swf.writeByte(22);
				swf.writeByte(3);
				swf.writeByte(24);
				swf.writeByte(2);
				swf.writeByte(5);
				swf.writeByte(0);
				swf.writeByte(22);
				swf.writeByte(7);
				swf.writeByte(24);
				swf.writeByte(6);
				swf.writeByte(22);
				swf.writeByte(10);
				swf.writeByte(2);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(12);
				swf.writeByte(7);
				swf.writeByte(1);
				swf.writeByte(2);
				swf.writeByte(7);
				swf.writeByte(2);
				swf.writeByte(4);
				swf.writeByte(7);
				swf.writeByte(4);
				swf.writeByte(5);
				swf.writeByte(7);
				swf.writeByte(1);
				swf.writeByte(6);
				swf.writeByte(7);
				swf.writeByte(5);
				swf.writeByte(8);
				swf.writeByte(7);
				swf.writeByte(1);
				swf.writeByte(9);
				swf.writeByte(7);
				swf.writeByte(7);
				swf.writeByte(11);
				swf.writeByte(7);
				swf.writeByte(2);
				swf.writeByte(12);
				swf.writeByte(7);
				swf.writeByte(2);
				swf.writeByte(13);
				swf.writeByte(7);
				swf.writeByte(2);
				swf.writeByte(14);
				swf.writeByte(9);
				swf.writeByte(6);
				swf.writeByte(1);
				swf.writeByte(6);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(2);
				swf.writeByte(1);
				swf.writeByte(2);
				swf.writeByte(9);
				swf.writeByte(3);
				swf.writeByte(0);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(3);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(4);
				swf.writeByte(0);
				swf.writeByte(4);
				swf.writeByte(5);
				swf.writeByte(9);
				swf.writeByte(6);
				swf.writeByte(0);
				swf.writeByte(4);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(3);
				swf.writeByte(0);
				swf.writeByte(2);
				swf.writeByte(2);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(4);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(5);
				swf.writeByte(1);
				swf.writeByte(4);
				swf.writeByte(4);
				swf.writeByte(0);
				swf.writeByte(1);
				swf.writeByte(6);
				swf.writeByte(0);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(8);
				swf.writeByte(9);
				swf.writeByte(3);
				swf.writeByte(208);
				swf.writeByte(48);
				swf.writeByte(71);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(9);
				swf.writeByte(10);
				swf.writeByte(6);
				swf.writeByte(208);
				swf.writeByte(48);
				swf.writeByte(208);
				swf.writeByte(73);
				swf.writeByte(0);
				swf.writeByte(71);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(2);
				swf.writeByte(2);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(8);
				swf.writeByte(35);
				swf.writeByte(208);
				swf.writeByte(48);
				swf.writeByte(101);
				swf.writeByte(0);
				swf.writeByte(96);
				swf.writeByte(6);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(7);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(8);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(9);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(10);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(2);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(2);
				swf.writeByte(88);
				swf.writeByte(0);
				swf.writeByte(29);
				swf.writeByte(29);
				swf.writeByte(29);
				swf.writeByte(29);
				swf.writeByte(29);
				swf.writeByte(29);
				swf.writeByte(104);
				swf.writeByte(1);
				swf.writeByte(71);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(3);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(5);
				swf.writeByte(6);
				swf.writeByte(3);
				swf.writeByte(208);
				swf.writeByte(48);
				swf.writeByte(71);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(4);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(6);
				swf.writeByte(7);
				swf.writeByte(6);
				swf.writeByte(208);
				swf.writeByte(48);
				swf.writeByte(208);
				swf.writeByte(73);
				swf.writeByte(0);
				swf.writeByte(71);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(5);
				swf.writeByte(2);
				swf.writeByte(1);
				swf.writeByte(1);
				swf.writeByte(5);
				swf.writeByte(23);
				swf.writeByte(208);
				swf.writeByte(48);
				swf.writeByte(93);
				swf.writeByte(11);
				swf.writeByte(96);
				swf.writeByte(6);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(7);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(5);
				swf.writeByte(48);
				swf.writeByte(96);
				swf.writeByte(5);
				swf.writeByte(88);
				swf.writeByte(1);
				swf.writeByte(29);
				swf.writeByte(29);
				swf.writeByte(29);
				swf.writeByte(104);
				swf.writeByte(4);
				swf.writeByte(71);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(30);
				swf.writeByte(19);
				swf.writeByte(2);
				swf.writeByte(0);
				swf.writeByte(1);
				swf.writeByte(0);
				swf.writeByte(83);
				swf.writeByte(111);
				swf.writeByte(117);
				swf.writeByte(110);
				swf.writeByte(100);
				swf.writeByte(73);
				swf.writeByte(116);
				swf.writeByte(101);
				swf.writeByte(109);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(0);
				swf.writeByte(83);
				swf.writeByte(111);
				swf.writeByte(117);
				swf.writeByte(110);
				swf.writeByte(100);
				swf.writeByte(80);
				swf.writeByte(114);
				swf.writeByte(111);
				swf.writeByte(118);
				swf.writeByte(105);
				swf.writeByte(100);
				swf.writeByte(101);
				swf.writeByte(114);
				swf.writeByte(0);
				
				swf.endian = Endian.LITTLE_ENDIAN;
				swf.position = swf.length;
				
				//-- write define sound tag header
				swf.writeShort( 0x3bf );
				swf.writeUnsignedInt( bytes.length + 7 );
				
				//-- assemble audio property byte (uncompressed little endian)
				var byte2: uint = 3 << 4;
				
				var rate:uint = 44100;
				
				switch( rate )
				{
					case 44100: byte2 |= 0xc; break;
					case 22050: byte2 |= 0x8; break;
					case 11025:	byte2 |= 0x4; break;
				}
				
				var numSamples: int = bytes.length;
				
				var channels:uint = 2
				
				if( channels == 2 )
				{
					byte2 |= 1;
					numSamples >>= 1;
				}
				
				var bits:uint = 16;
				
				if( bits == 16 )
				{
					byte2 |= 2;
					numSamples >>= 1;
				}
				
				//-- write define sound tag
				swf.writeShort( 1 );
				swf.writeByte( byte2 );
				swf.writeUnsignedInt( numSamples );
				swf.writeBytes( bytes );
				
				//-- write eof tag in swf stream
				swf.writeShort( 1 << 6 );
				
				//-- overwrite swf length
				swf.position = 4;
				swf.writeUnsignedInt( swf.length );
				swf.position = 0;
				
				
				
				var onSWFLoaded: Function = function( event: Event ): void
				{
					var sound:Sound = Sound( new ( loader.contentLoaderInfo.applicationDomain.getDefinition( 'SoundItem' ) as Class )() );
					
					var channel:SoundChannel = sound.play();
					if(channel != null){
						_soundEnabled = true;
					}else{
						_soundEnabled = false;
					}
					
					_init = true;
				};
				
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onSWFLoaded );
				loader.loadBytes( swf );
			}
		}
		public static function get soundEnabled():Boolean{
			return _soundEnabled;
		}
	}
}